//通过mutations间接更新state的多个方法的对象
import  {
    RECIVE_ADDRESS,
    RECIVE_CATEGORYS,
    RECIVE_SHOPS,
    RECIVE_USER_INFO,
    RESET_USER_INFO,
    RECIVE_RATINGS,
    RECIVE_INFO,
    RECIVE_GOODS,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_CART,
    RECIVE_SEARCH_SHOPS
} from './mutation-types.js'
import  {
    reqAddress,
    reqFoodCategorys,
    reqShops,
    reqUserInfo,
    reqLogout,
    reqShopGoods,
    reqShopInfo,
    reqShopRatings,
    reqSearchShop
} from '../api/index.js'
export default {
//异步获取地址
    async getAddress({commit,state}){
        const geohash =state.latitude+","+state.longitude ;
        const result=await reqAddress(geohash);
        if(result.code===0){
            const address=result.data;
           commit(RECIVE_ADDRESS,{address})
        }
    },
//异步获取食品分类
    async getCategorys({commit,state}){
        //先获取食品分类列表，然后将列表通过mutation修改state
        const result=await reqFoodCategorys();
        if(result.code===0){
            const categorys=result.data;
            commit(RECIVE_CATEGORYS,{categorys})
        }
    },
//异步获取商铺信息

    async getShops({commit,state}){
        const latitude=state.latitude;
        const longitude=state.longitude;
        const result=await reqShops({latitude,longitude}) ;
        if(result.code===0){
            const shops=result.data;
            commit(RECIVE_SHOPS,{shops})
        }
    },

    //同步记录用户信息
    recordUser({commit,state},userInfo){
            // const id=user.id;
            // let name;
            // let phone;
            // if(user.name){
            //     name=user.name;
            // }
            // if(user.phone){
            //     phone=user.phone
            // }
            commit(RECIVE_USER_INFO,{userInfo})
    },
    //获取用户信息
    async getUserInfo({commit,state}){
        const result=await reqUserInfo();
        if(result.code===0){
            const userInfo=result.data;
            commit(RECIVE_USER_INFO,{userInfo})
        }

    },
    //用户登出
    async resetUserInfo({commit}){
        const result=await reqLogout();
        if(result.code===0){
            commit(RESET_USER_INFO,{})
        }
    },
    //获取店铺食品分类推荐goods
    async getShopGoods({commit},callback){
        const result =await reqShopGoods();
        if(result.code===0){
            const goods=result.goods;
            commit(RECIVE_GOODS,{goods});
            callback&&callback()
        }

    },
    //获取店铺评论
    async getShopRatings({commit,state},{id,callback}){
        const result=await reqShopRatings({id});
        if(result.code===0){
            const ratings=result.ratings;
            commit(RECIVE_RATINGS,{ratings});
            callback&&callback();
        }
    },
    //获取店铺内部信息
    async getShopInfo({commit,state}){
        const result =await reqShopInfo();
        if(result.code===0){
            const info=result.info;
            commit(RECIVE_INFO,{info})
        }
    },
    //更新food的信息
    updateFoodCount({commit,state},{isAdd,food}){
        if(isAdd){//增加一个数量
            commit(INCREMENT_FOOD_COUNT,{food})
        }else{
            commit(DECREMENT_FOOD_COUNT,{food})
        }
    },
    //清空购物车
    clearCart({commit,state}){
        commit(CLEAR_CART)

    },
    //搜索店铺

    async searchShop({commit,state},keyword){
        const geohash=state.latitude+","+state.longitude;

        const result = await reqSearchShop(geohash, keyword);
        console.log(result);
        if(result.code===0){
            const searchShops=result.data;
            commit(RECIVE_SEARCH_SHOPS,{searchShops})
        }


    }

}




















